//
//  HomePageTableViewCell.m
//  Taste
//
//  Created by 徐昊 on 16/8/31.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "HomePageTableViewCell.h"
#import "ImageAndLabelCollectionViewCell.h"
#import "ImageCollectionViewCell.h"
#import "FatCollectionViewCell.h"
#import "HXMacro.h"
#import "Content.h"
#import "ShowDetailViewController.h"
#import "HomepageViewController.h"
#import "PersonViewController.h"
#import "HomePageDetailViewController.h"
#import "RestaurantDetailViewController.h"
#import "MasterCookBookViewController.h"

#define kHomePageCollectionViewImageCellName @"HomePageCollectionViewImageCellNameReuse"
#define kHomePageCollectionViewImageAndLabelCellName @"HomePageCollectionViewImageAndLabelCellReuse"
#define kHomePageCollectionViewFatCellName @"HomePageCollectionViewFatCellNameReuse"

@interface HomePageTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;



@end

@implementation HomePageTableViewCell

- (void)setArray:(NSMutableArray<Content *> *)array {
    _array = array;
    [self.collectionView reloadData];
}
- (void)setNum:(NSInteger)num {
    _num = num;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    _layout.minimumInteritemSpacing = 5;
    _layout.minimumLineSpacing = 5;
    _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageAndLabelCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kHomePageCollectionViewImageAndLabelCellName];
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kHomePageCollectionViewImageCellName];
    [_collectionView registerNib:[UINib nibWithNibName:@"FatCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kHomePageCollectionViewFatCellName];
    [self.contentView addSubview:_collectionView];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_num == 0) {
        _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 20) / 3, self.bounds.size.height - 10);
        return _layout.itemSize;
    } else if (_num == 1 || _num == 4 || _num == 6) {
        _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 20) / 3, self.bounds.size.height - 10);
        return _layout.itemSize;
    } else {
        _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 15) / 2, self.bounds.size.height - 10);
        return _layout.itemSize;
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_num == 0) {
        ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHomePageCollectionViewImageCellName forIndexPath:indexPath];
        if (_array) {
            cell.content = _array[indexPath.row];
        }
        return cell;
    } else if (_num == 1 || _num == 4 || _num == 6)
    {
        
        ImageAndLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHomePageCollectionViewImageAndLabelCellName forIndexPath:indexPath];
        if (_array) {
            cell.content = _array[indexPath.row];
        }
        return cell;
    } else {
        FatCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHomePageCollectionViewFatCellName forIndexPath:indexPath];
        if (_array) {
            cell.content = _array[indexPath.row];
        }
        return cell;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([_array[indexPath.row].cid2 isEqualToString:@"3"]) {
        ShowDetailViewController *vc = [[ShowDetailViewController alloc] init];
        HomepageViewController *vcOfHomePage = (HomepageViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
        vc.idString = _array[indexPath.row].sid;
        [vcOfHomePage.navigationController pushViewController:vc animated:YES];
    }
    if ([_array[indexPath.row].cid2 isEqualToString:@"16"]) {
        PersonViewController *vc = [[PersonViewController alloc] init];
        HomepageViewController *vcOfHomePage = (HomepageViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
        vc.numString = _array[indexPath.row].sid;
        [vcOfHomePage.navigationController pushViewController:vc animated:YES];
    }
    if ([_array[indexPath.row].cid2 isEqualToString:@"1"]) {
        HomePageDetailViewController *vc = [[HomePageDetailViewController alloc] init];
        HomepageViewController *vcOfHomePage = (HomepageViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
        vc.numString = _array[indexPath.row].sid;
        [vcOfHomePage.navigationController pushViewController:vc animated:YES];
    }
    if ([_array[indexPath.row].cid2 isEqualToString:@"15"]) {
        RestaurantDetailViewController *vc = [[RestaurantDetailViewController alloc] init];
        HomepageViewController *vcOfHomePage = (HomepageViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
        vc.numString = _array[indexPath.row].sid;
        [vcOfHomePage.navigationController pushViewController:vc animated:YES];
    }
    if ([_array[indexPath.row].cid2 isEqualToString:@"14"]) {
        MasterCookBookViewController *vc =[[MasterCookBookViewController alloc] init];
        HomepageViewController *vcOfHomePage = (HomepageViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
        vc.numString = _array[indexPath.row].sid;
        [vcOfHomePage.navigationController pushViewController:vc animated:YES];
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
